package cn.zxh.csdn_maven.atomy.auth;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import cn.zxh.csdn_maven.base.pojo.Cookies;

/**
 * 下载
 * 
 * @author 87077
 * 
 */
@SuppressWarnings("unused")
public class DownLoad {

	/**
	 * 
	 * @param urlPath 远程地址
	 * @param downloadDir 本地地址
	 * @throws IOException
	 */
	public static void startDownLoad(String urlPath , String downloadDir) throws IOException {
		startDownLoad(urlPath, downloadDir,Cookies.getCookies(),"test.rar");
	}
	
	public static void startDownLoad(String urlPath , String downloadDir,String cookies,String fileFullName) throws IOException {
		File file = null;
		try {
			// 统一资源
			URL url = new URL(urlPath);
			// 连接类的父类，抽象类
			URLConnection urlConnection = url.openConnection();
			// http的连接类
			HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
			// 设定请求的方法，默认是GET
			httpURLConnection.setRequestMethod("GET");
			
			httpURLConnection.setRequestProperty("Cookie", cookies);
			// 设置字符编码
			httpURLConnection.setRequestProperty("Charset", "UTF-8");
			// 打开到此 URL 引用的资源的通信链接（如果尚未建立这样的连接）。
			httpURLConnection.connect();

			// 文件大小
			int fileLength = httpURLConnection.getContentLength();

			// 文件名
			String filePathUrl = httpURLConnection.getURL().getFile();
			System.out.println("file length---->" + fileLength);

			URLConnection con = url.openConnection();

			BufferedInputStream bin = new BufferedInputStream(
					httpURLConnection.getInputStream());

			String path = downloadDir + File.separatorChar + fileFullName;
			file = new File(path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			OutputStream out = new FileOutputStream(file);
			int size = 0;
			int len = 0;
			byte[] buf = new byte[1024];
			while ((size = bin.read(buf)) != -1) {
				len += size;
				out.write(buf, 0, size);
				// 打印下载百分比
				 System.out.println("下载了-------> " + len * 100 / fileLength + "%\n");
			}
			bin.close();
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
//			return file;
		}
	}
	public void startDownLoadByClient(){
		
	}
	
	private class zxhtest{
		
	}
}
